package com.example.allplan.Fragment;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.allplan.Activity.DaoJiShiActivity;
import com.example.allplan.Activity.TianJiaNaoZhongActivity;
import com.example.allplan.JavaBean.NaoZhongJavaBean;
import com.example.allplan.MyApplication;
import com.example.allplan.R;
import com.example.allplan.RecyclerAdapter.NaoZhongLieBiaoAdapter;
import com.example.allplan.StaticFangfa.BiaoQingZhuanHuan;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.Calendar;

public class NaoZhongFragment extends BaseFragment implements View.OnClickListener {
    View mview;
    TextView naozhongtubiao_textview;
    ImageView wushujuxianshi_imageview;
    RecyclerView naozhongliebiao_recyclerview;
    Toolbar mtoolbar;
    FloatingActionButton floatbutton;
    ArrayList<NaoZhongJavaBean> mlist;
    NaoZhongLieBiaoAdapter madapter;
    MyApplication myapp;
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater,container,savedInstanceState);
        mview=inflater.inflate(R.layout.nao_zhong_fragment_layout,container,false);
        init();
        return mview;

    }



    void init(){
        naozhongtubiao_textview=mview.findViewById(R.id.nao_zhong_biao_ti_lan_xian_shi_biao_qing_TextView);
        wushujuxianshi_imageview=mview.findViewById(R.id.nao_zhong_wu_shu_ju_xian_shi_de_tu_imageView);
        naozhongliebiao_recyclerview=mview.findViewById(R.id.nao_zhong_lie_biao_recyclerView);
        mtoolbar=mview.findViewById(R.id.nao_zhong_toolbar);
        naozhongtubiao_textview.setText(BiaoQingZhuanHuan.biao_qing_int_to_string(9200));
        floatbutton=mview.findViewById(R.id.tian_jia_nao_zhong_floatbutton);
        floatbutton.setOnClickListener(this);
        myapp=MyApplication.getInstance();
    }
    void reload(){
        lazyLoadData();
    }

    void shezhixitongnaozhong(NaoZhongJavaBean o,Context mcon){
        Calendar now=Calendar.getInstance();
        int nowint=now.get(Calendar.HOUR_OF_DAY)*60+now.get(Calendar.MINUTE);
        if(nowint<o.getKai_shi_zhi()||nowint>o.getJie_shu_zhi()){
            Toast.makeText(myapp, "不在时间区间", Toast.LENGTH_SHORT).show();
        }else {
            int result=o.getChi_xu_zhi()+nowint;
            if(result>o.getJie_shu_zhi()){
                result=o.getJie_shu_zhi();
            }
            if(result%60==40||result%60==0||result%60==30||result%60==5||result%60==50||result%60==20)
                result++;
            Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
            intent.putExtra(AlarmClock.EXTRA_MESSAGE,o.getNao_zhong_ming());
            intent.putExtra(AlarmClock.EXTRA_HOUR, result/60);
            intent.putExtra(AlarmClock.EXTRA_MINUTES, result%60);
            if (Build.VERSION.SDK_INT >= 11) {
                intent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
            }
            if (Build.VERSION.SDK_INT >= 19) {
                intent.putExtra(AlarmClock.EXTRA_VIBRATE, true);
            }
            try {
                mcon.startActivity(intent);
                return ;
            } catch (Exception e) {
                e.printStackTrace();
                return ;
            }

        }

    }
    protected void lazyLoadData() {
        mlist=myapp.mNaoZhongCaoZuo.readall();
        if(mlist.size()>0){
            wushujuxianshi_imageview.setVisibility(View.GONE);
        }else {
            wushujuxianshi_imageview.setVisibility(View.VISIBLE);
        }
        madapter=new NaoZhongLieBiaoAdapter(mlist, getActivity(), new NaoZhongLieBiaoAdapter.onItemClickListener() {
            @Override
            public void onXiTongClick(int pos, Context mcon) {
                shezhixitongnaozhong(mlist.get(pos),mcon);
            }

            @Override
            public void onDaoJiShiClick(int pos,Context mcon) {
                dakaidaojishi(mlist.get(pos),mcon);
            }

            @Override
            public void ondeleteClick(int pos) {
                    chushihuadialog(mlist.get(pos),pos);
            }
        });
        naozhongliebiao_recyclerview.setAdapter(madapter);
        naozhongliebiao_recyclerview.setLayoutManager(new GridLayoutManager(getActivity(),2));
    }


    void dakaidaojishi(NaoZhongJavaBean o,Context mcon){
        Intent min=new Intent(mcon, DaoJiShiActivity.class);
        min.putExtra("time",o.getChi_xu_zhi());
        mcon.startActivity(min);
    }
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.tian_jia_nao_zhong_floatbutton){
            Intent min=new Intent(getActivity(), TianJiaNaoZhongActivity.class);
            startActivityForResult(min,81);
        }
    }
    void chushihuadialog(final NaoZhongJavaBean o, final int pos){
        AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle("选择")
                .setMessage("是否删除？")
                .setCancelable(false)
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       myapp.mNaoZhongCaoZuo.deleteone(o);
                       myapp.putSH_Int(myapp.NAO_ZHONG_SHU_SHARE_NAME,--myapp.nao_zhong_ge_shu);
                      reload();
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(myapp, "取消", Toast.LENGTH_SHORT).show();
                    }
                })
                .create();
        dialog.show();
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.lan_se));
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.lan_se));
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==81&&resultCode==82){
            reload();
        }
    }
}
